package com.weather.business.service.impl;

import com.weather.business.domain.NmCrops;
import com.weather.business.domain.NmRule;
import com.weather.business.domain.WeatherData;
import com.weather.business.mapper.NmCropsMapper;
import com.weather.business.mapper.NmRuleMapper;
import com.weather.business.mapper.WeatherDataMapper;
import com.weather.business.service.INmCropsService;
import com.weather.common.core.domain.entity.SysUser;
import com.weather.common.utils.DateUtils;
import com.weather.common.utils.SecurityUtils;
import com.weather.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 养殖日期Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-16
 */
@Service
public class NmCropsServiceImpl implements INmCropsService
{
    @Autowired
    private NmCropsMapper nmCropsMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private NmRuleMapper nmRuleMapper;
    @Autowired
    private WeatherDataMapper weatherDataMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询养殖日期
     *
     * @param id 养殖日期主键
     * @return 养殖日期
     */
    @Override
    public NmCrops selectNmCropsById(Long id)
    {
        return nmCropsMapper.selectNmCropsById(id);
    }

    /**
     * 查询养殖日期列表
     *
     * @param nmCrops 养殖日期
     * @return 养殖日期
     */
    @Override
    public List<NmCrops> selectNmCropsList(NmCrops nmCrops)
    {
        List<NmCrops> list = nmCropsMapper.selectNmCropsList(nmCrops);

        for (NmCrops crops :list){
            //查询用户所在城市
            SysUser user = userMapper.selectUserById(SecurityUtils.getUserId());
            String cityid = user.getCityid();
            //查询城市当日天气
            WeatherData weatherData = new WeatherData();
            weatherData.setCityid(cityid);
            weatherData.setDate(DateUtils.parseDateToStr("yyyy-MM-dd",crops.getPlantingDate()));
            List<WeatherData> weather = weatherDataMapper.selectWeatherDataList(weatherData);
            //匹配后台规则
            if (weather.size()>0) {
                NmRule rule = new NmRule();
                rule.setCropsType(crops.getCropsType());
                rule.setWea(weather.get(0).getWea());
                rule.setHumidity(weather.get(0).getHumidity().substring(0,2));
                rule.setTem(weather.get(0).getTem());
                rule.setWinSpeed(weather.get(0).getWinSpeed());
                List<NmRule> nmRules = nmRuleMapper.selectRecommend(rule);
                if (nmRules.size()>0) {
                    crops.setRecommend(nmRules.get(0).getRecommendation());
                }
            }
        }
        return list;
    }

    /**
     * 新增养殖日期
     *
     * @param nmCrops 养殖日期
     * @return 结果
     */
    @Override
    public int insertNmCrops(NmCrops nmCrops)
    {
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        nmCrops.setCityid(sysUser.getCityid());
        nmCrops.setUserId(SecurityUtils.getUserId());
        nmCrops.setCreateTime(DateUtils.getNowDate());
        return nmCropsMapper.insertNmCrops(nmCrops);
    }

    /**
     * 修改养殖日期
     *
     * @param nmCrops 养殖日期
     * @return 结果
     */
    @Override
    public int updateNmCrops(NmCrops nmCrops)
    {
        nmCrops.setUpdateTime(DateUtils.getNowDate());
        return nmCropsMapper.updateNmCrops(nmCrops);
    }

    /**
     * 批量删除养殖日期
     *
     * @param ids 需要删除的养殖日期主键
     * @return 结果
     */
    @Override
    public int deleteNmCropsByIds(Long[] ids)
    {
        return nmCropsMapper.deleteNmCropsByIds(ids);
    }

    /**
     * 删除养殖日期信息
     *
     * @param id 养殖日期主键
     * @return 结果
     */
    @Override
    public int deleteNmCropsById(Long id)
    {
        return nmCropsMapper.deleteNmCropsById(id);
    }
}
